package com.test.controller;


import com.test.bean.*;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "book")
public class BookController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddCourse(Book book, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        try {
            userService.addBook(book);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.DELETE)
    public Result DeleteBook(Book book,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        int id = book.getId();
        Book dbook = userService.queryBook(id);
        if(dbook != null){
            try {
                userService.deleteBook(book);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }
}
